//
//  SYLJEmoticonAttachment.swift
//  Emoticon
//
//  Created by 刘俊 on 15/10/28.
//  Copyright © 2015年 刘俊. All rights reserved.
//

import UIKit

/// 表情附件
class SYLJEmoticonAttachment: NSTextAttachment {

    /// 表情模型
    var emoticon: Emoticon
    
    init(emoticon: Emoticon) {
        self.emoticon = emoticon
        
        super.init(data: nil, ofType: nil)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    /// 将当前附件中的 emoticon 转换成属性文本
    func imageText(font: UIFont) -> NSAttributedString {
   
        image = UIImage(contentsOfFile: emoticon.pngPath)
        // '线宽'表示字体的高度
        let lineHeight = font.lineHeight
        bounds = CGRect(x: 0, y: -4, width: lineHeight, height: lineHeight)
        let attributed = NSAttributedString(attachment: self)
        let strM = NSMutableAttributedString(attributedString: attributed)
        //添加`字体 - UIKit.framework 第一个头文件
        strM.addAttribute(NSFontAttributeName, value: font, range: NSRange(location: 0, length: 1))
        
        return strM
    }

}
